<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div #tbWidgetElement tb-fullscreen [fullscreen]="widget.isFullscreen"
     [fullscreenBackgroundStyle]="dashboardStyle"
     [fullscreenBackgroundImage]="backgroundImage"
     (fullscreenChanged)="onFullscreenChanged($event)"
     class="tb-widget"
     [class.tb-highlighted]="isHighlighted(widget)"
     [class.tb-not-highlighted]="isNotHighlighted(widget)"
     [class.mat-elevation-z4]="widget.dropShadow"
     [class.tb-overflow-visible]="widgetComponent.widgetContext?.overflowVisible"
     [class.tb-has-timewindow]="widget.hasTimewindow"
     [class.tb-edit]="isEdit || isEditingWidget"
     [class.tb-hover]="hovered"
     [style]="widget.style">
  <div *ngIf="!!widgetComponent.widgetContext?.inited"
       class="tb-widget-header">
    <ng-container *ngIf="!widgetComponent.widgetContext?.embedTitlePanel">
      <ng-container *ngTemplateOutlet="widgetTitlePanel"></ng-container>
    </ng-container>
    <ng-container *ngIf="!widgetComponent.widgetContext?.embedActionsPanel">
      <ng-container *ngTemplateOutlet="widgetHeaderActionsPanel"></ng-container>
    </ng-container>
  </div>
  <div class="tb-widget-content" [class.tb-no-interaction]="disableWidgetInteraction">
    <tb-widget #widgetComponent
               [dashboardWidget]="widget"
               [widget]="widget.widget"
               [isEdit]="isEdit"
               [isPreview]="isPreview"
               [isMobile]="isMobile"
               [widgetTitlePanel]="widgetTitlePanel"
               [widgetHeaderActionsPanel]="widgetHeaderActionsPanel">
    </tb-widget>
  </div>
</div>
<ng-template #widgetTitlePanel let-titleSuffixTemplate="titleSuffixTemplate">
  <div *ngIf="widget.showWidgetTitlePanel"
       class="tb-widget-title">
              <div *ngIf="widget.showTitle"
                    [matTooltip]="widget.titleTooltip"
                    matTooltipClass="tb-tooltip-multiline"
                    matTooltipPosition="above"
                    class="title-row">
                <tb-icon *ngIf="widget.showTitleIcon" [style]="widget.titleIconStyle">{{widget.titleIcon}}</tb-icon>
                <div class="mat-subtitle-1 title" [style]="widget.titleStyle">
                  {{widget.title$ | async}}
                </div>
                <ng-container *ngTemplateOutlet="titleSuffixTemplate"></ng-container>
              </div>
    <tb-timewindow *ngIf="widget.hasTimewindow"
                   aggregation="{{widget.hasAggregation}}"
                   quickIntervalOnly="{{widget.onlyQuickInterval}}"
                   historyOnly="{{widget.onlyHistoryTimewindow}}"
                   alwaysDisplayTypePrefix
                   timezone="true"
                   noMargin
                   noPadding
                   [isEdit]="isEdit"
                   [timewindowStyle]="widget.widget.config.timewindowStyle"
                   [(ngModel)]="widget.widget.config.timewindow"
                   (ngModelChange)="widgetComponent.onTimewindowChanged($event)">
    </tb-timewindow>
  </div>
</ng-template>

<ng-template #widgetHeaderActionsPanel let-absolute="absolute">
  <div *ngIf="widget.showWidgetActions"
       class="tb-widget-actions"
       [class.tb-widget-actions-absolute]="widgetActionAbsolute(widgetComponent, absolute)"
       (mousedown)="$event.stopPropagation()">
    <div class="flex items-center">
      @for (action of widget.customHeaderActions; track action.name; let last = $last) {
        <ng-container *ngTemplateOutlet="widgetHeaderActionButton; context:{ action, last }"></ng-container>
      }
    </div>

    <button mat-icon-button *ngFor="let action of widget.widgetActions"
            type="button"
            [class.!hidden]="isEdit || !action.show"
            (click)="action.onAction($event)"
            matTooltip="{{ action.name | translate }}"
            matTooltipPosition="above">
      <tb-icon>{{ action.icon }}</tb-icon>
    </button>
    <button mat-icon-button
            type="button"
            [class.!hidden]="isEdit || !widget.enableFullscreen"
            (click)="$event.stopPropagation(); widget.isFullscreen = !widget.isFullscreen; updateEditWidgetActionsTooltipState()"
            matTooltip="{{(widget.isFullscreen ? 'fullscreen.exit' : 'fullscreen.expand') | translate}}"
            matTooltipPosition="above">
      <tb-icon>{{ widget.isFullscreen ? 'fullscreen_exit' : 'fullscreen' }}</tb-icon>
    </button>
  </div>
</ng-template>

<ng-template #widgetHeaderActionButton let-action="action" let-last="last">
@switch (action.buttonType) {
  @case (widgetHeaderActionButtonType.miniFab) {
    <button mat-mini-fab
            [style]="action.customButtonStyle"
            [class.!hidden]="isEdit"
            [class.mr-2]="!last"
            (click)="action.onAction($event)"
            matTooltip="{{ action.displayName }}"
            matTooltipPosition="above">
      <tb-icon>{{ action.icon }}</tb-icon>
    </button>
  }
  @case (widgetHeaderActionButtonType.basic) {
    <button [class.!hidden]="isEdit" mat-button
            [class.mr-2]="!last"
            [style]="action.customButtonStyle"
            (click)="action.onAction($event)"
            matTooltip="{{ action.displayName }}"
            matTooltipPosition="above">
      <tb-icon matButtonIcon *ngIf="action.showIcon">{{ action.icon }}</tb-icon>
      <span>{{ action.displayName }}</span>
    </button>
  }
  @case (widgetHeaderActionButtonType.raised) {
    <button [class.!hidden]="isEdit" mat-raised-button
            [class.mr-2]="!last"
            [style]="action.customButtonStyle"
            (click)="action.onAction($event)"
            matTooltip="{{ action.displayName }}"
            matTooltipPosition="above">
      <tb-icon matButtonIcon *ngIf="action.showIcon">{{ action.icon }}</tb-icon>
      <span>{{ action.displayName }}</span>
    </button>
  }
  @case (widgetHeaderActionButtonType.stroked) {
    <button [class.!hidden]="isEdit" mat-stroked-button
            [class.mr-2]="!last"
            [style]="action.customButtonStyle"
            (click)="action.onAction($event)"
            matTooltip="{{ action.displayName }}"
            matTooltipPosition="above">
      <tb-icon matButtonIcon *ngIf="action.showIcon">{{ action.icon }}</tb-icon>
      <span>{{ action.displayName }}</span>
    </button>
  }
  @case (widgetHeaderActionButtonType.flat) {
    <button [class.!hidden]="isEdit" mat-flat-button
            [class.mr-2]="!last"
            [style]="action.customButtonStyle"
            (click)="action.onAction($event)"
            matTooltip="{{ action.displayName }}"
            matTooltipPosition="above">
      <tb-icon matButtonIcon *ngIf="action.showIcon">{{ action.icon }}</tb-icon>
      <span>{{ action.displayName }}</span>
    </button>
  }
  @default {
    <button mat-icon-button
            [style]="action.customButtonStyle"
            [class.!hidden]="isEdit"
            (click)="action.onAction($event)"
            matTooltip="{{ action.displayName }}"
            matTooltipPosition="above">
      <tb-icon>{{ action.icon }}</tb-icon>
    </button>
  }
}
</ng-template>
